const { executeQuery, baseQuery } = require('./mongo_util')
const collectionName = "Students";

function addcart(carts, onRetGot, onErrGot) {
    executeQuery(collectionName, (collection, callback) => {
        collection.insertOne({...carts.valus}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

function updatecart(carts, onRetGot, onErrGot) {
    let userid = carts.userid;

    executeQuery(collectionName, (collection, callback) => {
        collection.update({"userid":userid},{$set:{...carts}}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

function deletecart(carts, onRetGot, onErrGot) {
    let userid = carts.userid;

    executeQuery(collectionName, (collection, callback) => {
        collection.deleteMany({ userid:userid}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}


function selectcart(carts,onRetGot, onErrGot) {
    let limit = carts.size*1;
    let skip = (carts.page - 1) * limit;
    console.log("shu",limit,skip);
    executeQuery(collectionName, (collection, callback) => {
        collection.find().count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find().skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}
function selectall(carts,onRetGot, onErrGot) {
    executeQuery(collectionName, (collection, callback) => {
        collection.find().count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find().toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

//echats 接口
function legend(carts,onRetGot, onErrGot) {
    executeQuery(collectionName, (collection, callback) => {
        collection.find().count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find().toArray().then((bbq => {
                const college = {
                    xk: 0,
                    qc: 0,
                    jj: 0,
                    tm: 0,
                    sj: 0,
                    jx: 0,
                    qt: 0,

                };
                bbq.map((item) => {
                    switch (item.college) {
                        case "信息科学与工程学院": college.xk=college.xk + 1;break
                        case "汽车工程学院": college.qc=college.qc + 1;break
                        case "经济管理学院": college.jj=college.jj + 1;break
                        case "土木建筑工程学院": college.tm=college.tm + 1;break
                        case "设计艺术学院": college.sj=college.sj + 1;break
                        case "机械工程学院": college.jx = college.jx + 1; break
                        default :college.qt = college.qt + 1;
                    }
                })

                console.log(college);
                obj.data = college;
                
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

// 查询单条数据
function list(carts,onRetGot, onErrGot) {
    const field = carts.field;
    const inputx = carts.input;
    // console.log("jiayou ",field,inputx);
    executeQuery(collectionName, (collection, callback) => {
        collection
            .find({ [field]:inputx}).toArray()
            .then((result) => {
                onRetGot(result);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
}
module.exports = {
    addcart,
    updatecart,
    deletecart,
    selectcart,
    selectall,
    list,
    legend
}